package com.android.datn.repositories;

import java.util.ArrayList;

import com.android.datn.objects.Album;
import com.android.datn.objects.Artist;
import com.android.datn.objects.Track;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.provider.MediaStore;

public class MusicRepository {
    private static final ArrayList<Track> NO_TRACKS_FOUND = new ArrayList<Track>();
    private static final ArrayList<Album> NO_ALBUMS_FOUND = new ArrayList<Album>();
    private static final ArrayList<Artist> NO_ARTIST_FOUND = new ArrayList<Artist>();
    private final static String[] TRACK_COLUMNS = {
        MediaStore.Audio.Media._ID,
        MediaStore.Audio.Media.DISPLAY_NAME,
        MediaStore.Audio.Media.DURATION,
        MediaStore.Audio.Media.ARTIST,
        MediaStore.Audio.Media.TITLE,
        MediaStore.Audio.Media.TRACK,
        MediaStore.Audio.Media.DATA };
    
    private final static String[] ALBUM_COLUMNS = { 
        MediaStore.Audio.Albums._ID, 
        MediaStore.Audio.Albums.ARTIST,
        MediaStore.Audio.Albums.ALBUM, 
        MediaStore.Audio.Albums.NUMBER_OF_SONGS};

    private final static String[] ARTIST_COLUMS = {
    	 MediaStore.Audio.Artists._ID,
    	 MediaStore.Audio.Artists.ARTIST,
    	 MediaStore.Audio.Artists.NUMBER_OF_TRACKS
    } ;
    public Track findTracksId(Context context, long trackId) {
        ArrayList<Track> tracks = findTracks(context, trackId, null);
        
        if( tracks.size() == 0) {
            return null;
        }
        
        return tracks.get(0);
    }
    
    public ArrayList<Track> findTracksByAlbumId(Context context, Long albumId) {
        return findTracks(context, null, albumId);
    }
    
	public ArrayList<Track> findTracks(Context context, Long trackId, Long albumId) {
		
		String where = null;
		String[] selectionArgs = null;
		String orderBy = MediaStore.Audio.Media.DEFAULT_SORT_ORDER;
		if( albumId != null) {
			where = MediaStore.Audio.Media.ALBUM_ID + " = ?";
			selectionArgs = new String[]{ albumId.toString() };
			orderBy = MediaStore.Audio.Media.TRACK;
		} else if ( trackId != null ){
            where = MediaStore.Audio.Media._ID + " = ?";
            selectionArgs = new String[]{ trackId.toString() };
            orderBy = MediaStore.Audio.Media.TRACK;
		}

    	ContentResolver contentResolver = context.getContentResolver();
		Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, TRACK_COLUMNS, where, selectionArgs, orderBy);
        
        if( cursor == null) {
            return NO_TRACKS_FOUND;
        }
        
        int idColumn = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
        int artistColumn = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
        int titleColumn = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
        int trackColumn = cursor.getColumnIndex(MediaStore.Audio.Media.TRACK);
        int durationColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
        int displayNameColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME);
        int dataColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);

        ArrayList<Track> tracks = new ArrayList<Track>();
        int index = 0;
        while( cursor.moveToNext()) {
            Track track = new Track(cursor.getLong(idColumn));
            track.setTitle(cursor.getString(titleColumn));
            track.setArtist(cursor.getString(artistColumn));
            track.setDuration(cursor.getInt(durationColumn));
            track.setTrackNumber(cursor.getInt(trackColumn));
            track.setLink(cursor.getString(dataColumn));
            track.setCode(null);
            track.setCreated(null);
            track.setDescription(null);
            track.setIdCategory(-1);
            track.setListen("0");
            track.setListening(false);
            track.setPercentCurrent(0);
            track.setPrepared(false);
            track.setRank(null);
            track.setType(0);
            track.setTypes(null);
            track.setVote("0");
            
            tracks.add(track);
        }
        
        cursor.close();
        return tracks;
	}

	public Album findAlbumById(Activity activity, Long albumId) {

		ArrayList<Album> albums = findAlbumsFilteredBy(activity, albumId);
	
		if( albums.size() > 0) {
			return null;
		}
		
		return albums.get(0);
	}

	private ArrayList<Album> findAlbumsFilteredBy(Context context, Long albumId) {
		String where = null;
		String[] selectionArgs = null;
		if( albumId != null ) {
			where = MediaStore.Audio.Albums._ID + " = ?";
			selectionArgs = new String[]{ albumId.toString()};
		}
		
		ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = contentResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, ALBUM_COLUMNS, where, selectionArgs, MediaStore.Audio.Albums.DEFAULT_SORT_ORDER);
        if (cursor == null) {
            return NO_ALBUMS_FOUND;
        }

        int idColumn = cursor.getColumnIndex(MediaStore.Audio.Albums._ID);
        int artistColumn = cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST);
        int albumColumn = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
        int trackCountColumn = cursor.getColumnIndex(MediaStore.Audio.Albums.NUMBER_OF_SONGS);
        
        ArrayList<Album> albums = new ArrayList<Album>();
        int index = 0;
        while (cursor.moveToNext()) {
            Album album = new Album( cursor.getLong(idColumn));
            album.setArtist( cursor.getString(artistColumn));
            album.setTitle( cursor.getString(albumColumn));
            album.setSong(String.valueOf(cursor.getInt(trackCountColumn)));
            
            albums.add(album);
        }
        
        cursor.close();
        return albums;
	}
	

    public ArrayList<Album> findAllAlbums(Activity activity) {
    	return findAlbumsFilteredBy(activity, null);
    }
    private ArrayList<Artist> findArtistFilteredBy(Context context, Long artistId) {
		String where = null;
		String[] selectionArgs = null;
		if( artistId != null ) {
			where = MediaStore.Audio.Artists._ID + " = ?";
			selectionArgs = new String[]{ artistId.toString()};
		}
		
		ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = contentResolver.query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, ARTIST_COLUMS, where, selectionArgs, MediaStore.Audio.Artists.DEFAULT_SORT_ORDER);
        if (cursor == null) {
            return NO_ARTIST_FOUND;
        }

        int idColumn = cursor.getColumnIndex(MediaStore.Audio.Artists._ID);
        int artistColumn = cursor.getColumnIndex(MediaStore.Audio.Artists.ARTIST);
        int tracksColumn = cursor.getColumnIndex(MediaStore.Audio.Artists.NUMBER_OF_TRACKS);
        
        ArrayList<Artist> artists = new ArrayList<Artist>();
        int index = 0;
        while (cursor.moveToNext()) {
            Artist artist = new Artist(cursor.getLong(idColumn));
            artist.setArtist( cursor.getString(artistColumn));
            artist.setNumOfTracks( cursor.getLong(tracksColumn));
            
            artists.add(artist);
        }
        
        cursor.close();
        return artists;
	}
	

    public ArrayList<Artist> findAllArtist(Activity activity) {
    	return findArtistFilteredBy(activity, null);
    }
    
public ArrayList<Track> findTracksByArtist(Context context, Long artistId) {
		
		String where = null;
		String[] selectionArgs = null;
		String orderBy = MediaStore.Audio.Media.DEFAULT_SORT_ORDER;
		if ( artistId != null ){
            where = MediaStore.Audio.Media.ARTIST_ID + " = ?";
            selectionArgs = new String[]{ artistId.toString() };
            orderBy = MediaStore.Audio.Media.TRACK;
		}

    	ContentResolver contentResolver = context.getContentResolver();
		Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, TRACK_COLUMNS, where, selectionArgs, orderBy);
        
        if( cursor == null) {
            return NO_TRACKS_FOUND;
        }
        
        int idColumn = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
        int artistColumn = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
        int titleColumn = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
        int trackColumn = cursor.getColumnIndex(MediaStore.Audio.Media.TRACK);
        int durationColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
        int displayNameColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME);
        int dataColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);

        ArrayList<Track> tracks = new ArrayList<Track>();
        int index = 0;
        while( cursor.moveToNext()) {
            Track track = new Track(cursor.getLong(idColumn));
            track.setTitle(cursor.getString(titleColumn));
            track.setArtist(cursor.getString(artistColumn));
            track.setDuration(cursor.getInt(durationColumn));
            track.setTrackNumber(cursor.getInt(trackColumn));
            track.setLink(cursor.getString(dataColumn));
            track.setCode(null);
            track.setCreated(null);
            track.setDescription(null);
            track.setIdCategory(0);
            track.setListen("0");
            track.setListening(false);
            track.setPercentCurrent(0);
            track.setPrepared(false);
            track.setRank(null);
            track.setTypes(null);
            track.setType(0);
            track.setVote("0");
            
            tracks.add(track);
        }
        
        cursor.close();
        return tracks;
	}
}
